﻿using CPPEI.Coolzon.Designer.Event.NCL;
using CPPEI.Coolzon.Designer.Event.UI;
using CPPEI.Coolzon.Designtime.Metadata;
using CPPEI.Coolzon.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.Designer.Views
{
    /// <summary>
    /// Interaction logic for SolutionSelectView.xaml
    /// </summary>
    public partial class SolutionSelectView : Window
    {
        private List<SolutionViewModel> solutionModelList;
        private SolutionViewModel _selected;
        private bool _isLocalSolution = true;
        public SolutionSelectView()
        {
            InitializeComponent();
            solutionModelList = new List<SolutionViewModel>();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            NCLEventBus.Unregister(this);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            NCLEventBus.Register(this);
            NCLEventBus.Publish(new BeginGetSolutionListEvent(UIContext.Current.Token));
            UICommandManager.Register(this, UICustomCommand.AddSolution);
        }

        private void SelectSolution(Guid solutionID)
        {
            OpenSolutionThread.Start(solutionID);
            this.Dispatcher.Invoke(() =>
            {
                this.DialogResult = true;
            });
        }

        private void btnRetry_Click(object sender, RoutedEventArgs e)
        {
            NCLEventBus.Publish(new BeginGetSolutionListEvent(UIContext.Current.Token));
            _btnRetry.Visibility = Visibility.Collapsed;
            _ctlTbMessage.Visibility = System.Windows.Visibility.Visible;
            _ctlwpListView.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            SelectSolution();
        }

        private void cancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void NewSolution_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
        public void ReportProgress(Guid id, string name, int current, int total)
        {
            ProgressbarWindow.Instance.ReportProgress(current, total);
        }

        public void Completed(bool HasError, int successed, int total)
        {
            ProgressbarWindow.Instance.Complete();
            if (HasError)
            {
                this.Dispatcher.Invoke(() =>
                {
                    CustomMessageBox.ShowError("获取最新失败");
                });
                return;
            }
            SelectSolution(_selected.ID);
        }

        private void ListViewItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            SelectSolution();
        }

        private void SelectSolution()
        {
            if (_ctlwpListView.SelectedItem != null)
            {
                _selected = (SolutionViewModel)_ctlwpListView.SelectedItem;
                if (_selected.IsLocalExist)
                {
                    SelectSolution(_selected.ID);
                }
                else
                {
                    //下载方案
                    GetLatestVersionThread.Start(_selected.ID, ReportProgress, Completed);
                    ProgressbarWindow.Instance.ShowProgress(false);
                }
            }
            else
            {
                CustomMessageBox.ShowWarning("请选择方案");
            }
        }

        private void ReSizeColumnWidth()
        {
            Dispatcher.Invoke(() =>
            {
                GridView gv = _ctlwpListView.View as GridView;
                if (gv != null)
                {
                    foreach (GridViewColumn gvc in gv.Columns)
                    {
                        gvc.Width = gvc.ActualWidth;
                        gvc.Width = Double.NaN;
                    }
                }
            });
        }

        private void _cbLocal_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            FilterSolution();

            if (_ctlwpListView != null)
            {
                ReSizeColumnWidth();
            }
        }
        /// <summary>
        /// 只显示本地方案
        /// </summary>
        private void FilterSolution()
        {
            this.Dispatcher.Invoke(() =>
            {
                if (solutionModelList == null || solutionModelList.Count == 0)
                    return;
                _ctlwpListView.Items.Clear();
                if (_cbLocal.SelectedIndex == 0)
                {
                    for (int i = 0; i < solutionModelList.Count; i++)
                    {
                        SolutionViewModel model = solutionModelList[i];

                        if (model.IsLocalExist)
                        {
                            _ctlwpListView.Items.Add(model);
                        }
                    }
                }
                else if (_cbLocal.SelectedIndex == 1)
                {
                    for (int i = 0; i < solutionModelList.Count; i++)
                    {
                        SolutionViewModel model = solutionModelList[i];

                        if (!model.IsLocalExist)
                        {
                            _ctlwpListView.Items.Add(model);
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < solutionModelList.Count; i++)
                    {
                        SolutionViewModel model = solutionModelList[i];

                        _ctlwpListView.Items.Add(model);
                    }
                }
            });
        }
    }
}
